package tests;


import gameComponents.Board;
import gameComponents.ChessGame;
import gameComponents.Player;
import gamePieces.Piece;
import junit.framework.TestCase;


public class ChessGameMovesTest extends TestCase {
	
	// Sets up game scenario with user
	ChessGame game = new ChessGame();
	Player player1 = game.getPlayer1();
	Player player2 = game.getPlayer2();
	Board board = game.getBoard();
	public ChessGameMovesTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	/**
	 * Tests the valid movements of each piece in a chess game.
	 * @throws Exception
	 * @author Vivian Andreeva, Sabrina Gaudreau
	 */
	public void testChessMovesGame() throws Exception {
		
		//Add the pieces to each player
		game.addPlayer1Pieces();
		game.addPlayer2Pieces();
		placeOnBoard();
		
		/**
		 * This is a test of the move methods for the Queen, Bishop, Rook and Pawn.
		 * All of these moves are done from 1 player's side.
		 * @author Vivian Andreeva
		 */
			// Use pawn at (H7) move to (H6)
			testMovePawn(7,6,7,5);
			
			// Use rook at (H8) move to (H7)
			game.switchPlayer();
			Piece rook1 = game.getPiece(7, 7);
			assertEquals(rook1.toString(), "R");
			assertEquals(rook1.getX(), 7);
			assertEquals(rook1.getY(), 7);
			game.moveBRQ(7, 6, rook1);
			assertEquals(rook1.getX(), 7);
			assertEquals(rook1.getY(), 6);
			placeOnBoard();

			
			// Use pawn at (F7) move to (F5)
			game.switchPlayer();
			testMovePawn(5,6,5,5);
			
			// Use queen at (E8) move to (F7)
			game.switchPlayer();
			Piece queen1 = game.getPiece(4, 7);
			assertEquals(queen1.toString(), "Q");
			assertEquals(queen1.getX(), 4);
			assertEquals(queen1.getY(), 7);
			game.moveBRQ(5, 6, queen1);
			assertEquals(queen1.getX(), 5);
			assertEquals(queen1.getY(), 6);
			placeOnBoard();
			
			// Use pawn at (D7) move to (D6)
			game.switchPlayer();
			testMovePawn(3,6,3,5);
			
			// Use Bishop at (C8) to (D7)
			game.switchPlayer();
			Piece b1 = game.getPiece(2, 7);
			assertEquals(b1.toString(), "B");
			assertEquals(b1.getX(), 2);
			assertEquals(b1.getY(), 7);
			game.moveBRQ(3, 6, b1);
			assertEquals(b1.getX(), 3);
			assertEquals(b1.getY(), 6);
			placeOnBoard();
			
			//Test for getting the knight (is it in right position)
			game.switchPlayer();
			Piece knight1 = game.getPiece(1, 7);
			assertEquals(knight1.getX(), 1);
			assertEquals(knight1.getY(), 7);
			
			//Test for moving the knight
			assertFalse(knight1.checkIfLegal(1, 5)); //test for not able to move there
			assertTrue(knight1.checkIfLegal(0, 5)); //can we move there?
			game.moveKnight(0, 5, knight1);
			assertEquals(knight1.getX(), 0);
			assertEquals(knight1.getY(), 5);
			placeOnBoard();
			
			//Test the same stuff but for the opposite player
			Piece knight2 = game.getPiece(1, 0);
			assertEquals(knight2.getX(), 1);
			assertEquals(knight2.getY(), 0);
			
			//Test for moving the knight
			assertFalse(knight2.checkIfLegal(1, 5)); //test for not able to move there
			assertTrue(knight2.checkIfLegal(0, 2)); //can we move there?
			game.moveKnight(0, 2, knight2);
			assertEquals(knight2.getX(), 0);
			assertEquals(knight2.getY(), 2);
			placeOnBoard();
			
			//Test for getting the king (is it in right position)
			assertTrue(game.getCurrentPlayer().getId()==1);
			Piece king1 = game.getPiece(3, 7);
			assertEquals(king1.getX(), 3);
			assertEquals(king1.getY(), 7);
			
			//Test the move method for king (it should not move on its first turn)
			assertFalse(king1.checkIfLegal(5, 7)); //test for not able to move there
			assertTrue(king1.checkIfLegal(2, 7)); //can we move there?
			game.moveKing(2, 7, king1); 
			assertEquals(king1.getX(), 2);
			assertEquals(king1.getY(), 7);
			placeOnBoard();
			
			//Lets move a piece so we can move the king
			/**Piece pawn2 = game.getPiece(3, 6);
			game.movePawn(3, 5, pawn2);
			game.placeOnBoard(player1);
			board.print();
			assertTrue(king1.checkIfLegal(3, 6)); //can we move there?
			game.moveKing(3, 6, king1); //king should be able to move there since its empty
			assertEquals(king1.getX(), 3);
			//assertEquals(king1.getY(), 6); //This is not working because it say my king would be in check
											//some work needs to be done for check...**/
		}
		/**
		 * Helper method to test the moves of Pawn.
		 * @param x - old coordinate
		 * @param y - old coordinate
		 * @param newX - new coordinate
		 * @param newY - new coordinate
		 * @author - Vivian Andreeva
		 */
		public void testMovePawn(int x, int y, int newX, int newY) {
			Piece pawn1 = game.getPiece(x, y);
			assertEquals(pawn1.toString(), "P");
			assertEquals(pawn1.getX(), x);
			assertEquals(pawn1.getY(), y);
			game.movePawn(newX, newY, pawn1);
			assertEquals(pawn1.getX(), newX);
			assertEquals(pawn1.getY(), newY);
			placeOnBoard();
		}
		
		/**
		 * Helper method to place all the pieces on the board after they have been moved.
		 * @author Vivian Andreeva
		 */
		public void placeOnBoard() {
			game.placeOnBoard(player1);
			game.placeOnBoard(player2);
			board.print();
		}

}
